package com.tencent.wxcloudrun.controller;

import com.tencent.wxcloudrun.config.ApiResponse;
import com.tencent.wxcloudrun.dao.SysUserMapper;
import com.tencent.wxcloudrun.model.SysUser;
import com.tencent.wxcloudrun.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserService sysUserService;

    /*@PostMapping(value = "/login",produces = "application/json")
    public void login(@RequestBody JSONObject param) throws IOException {
        logger.info(param.toString());
        String code = param.getString("code");
        String appId ="wx89c3158007bc88a1";
        String appSecurt = "98da5f6944424c0550885c26091d6223";

        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appId+"&secret="+appSecurt+"&js_code="+code+"&grant_type=authorization_code";
        logger.info(url);
        String resp = HttpClient.get(url);
        logger.info(resp);

        JSONObject user = JSONObject.parseObject(resp);

        indexMapper.addOpen(user.getString("openid"),user.getString("session_key"));
    }*/

    @PostMapping("/add")
    public ApiResponse addUser (@RequestBody SysUser user){
        logger.info("start==");
        logger.info(user.toString());

        sysUserService.insertSelective(user);

        return ApiResponse.ok();
    }
}
